﻿/*
* Activity Tracking Log v0.1.1 - http://activitytrackinglog.codeplex.com
* Activity Tracking Log for ASP.Net & ASP.Net MVC
* Copyright (c) 2011 Vadim Burac
* Licensed under the Microsoft Public License (Ms-PL)
* http://activitytrackinglog.codeplex.com/license
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.SessionState;
using System.Web;
using System.Security.Cryptography;
using ActivityTrackingLog.Web.Helpers;
using ActivityTrackingLog.Data;
using ActivityTrackingLog.Utils;
using ActivityTrackingLog.Web.Utils;

namespace ActivityTrackingLog.Web.Handlers
{
    internal abstract class BaseAnalyticsHandler : BaseHandler, IHttpHandler
    {
        #region IHttpHandler Members

        public bool IsReusable
        {
            // Return false in case your Managed Handler cannot be reused for another request.
            // Usually this would be false in case you have some state information preserved per request.
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            string content = "";
            if (ValidateAccess(context))
            {
                var qp = this.GetQueryParams(context);
                switch (qp.EntityType)
                {
                    case HtmlHelper.EntityEnum.ActivityItem:
                        content = HtmlHelper.Render<ActivityItem>(qp.ViewType, qp.ReportType, qp.EmbedStyles, qp.PageIndex, qp.PageSize, qp.Columns, qp.UserId);
                        break;
                    case HtmlHelper.EntityEnum.UserAgentItem:
                        content = HtmlHelper.Render<UserAgentItem>(qp.ViewType, qp.ReportType, qp.EmbedStyles, qp.PageIndex, qp.PageSize, qp.Columns, qp.UserId);
                        break;
                }
            }
            context.Response.ContentType = GetContentType();
            context.Response.ContentEncoding = System.Text.UTF8Encoding.Default;
            context.Response.Write(content);
            context.Response.End();
        }

        #endregion

        #region helpers

        protected virtual QueryParams GetQueryParams(HttpContext context)
        {
            return new QueryParams(context);
        }

        protected virtual string GetContentType()
        {
            throw new NotImplementedException();
        }

        protected virtual bool ValidateAccess(HttpContext context)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
